package com.example.shopping;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

//import com.mayuhao.homework.R;

import java.util.ArrayList;
import java.util.List;

public class ShoppingMallActivity extends AppCompatActivity implements View.OnClickListener {

    DbHelper myHelper;
    private List<Goods> data = new ArrayList<>();
//    private Goods goods = new Goods();
    private EditText mEtName;
    private EditText mEtPrice;
    private EditText mEtNumber;
    private TextView mTvShow;
    private Button mBtnAdd;
    private Button mBtnQuery;
    private Button mBtnUpdate;
    private Button mBtnDelete;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopping_mall);
        myHelper = new DbHelper(this);
        init();
    }

    private void init() {
        mEtName = findViewById(R.id.et_name);
        mEtPrice = findViewById(R.id.et_price);
        mEtNumber = findViewById(R.id.et_number);
        mTvShow = findViewById(R.id.tv_show);
        mBtnAdd = findViewById(R.id.btn_add);
        mBtnQuery = findViewById(R.id.btn_query);
        mBtnUpdate = findViewById(R.id.btn_update);
        mBtnDelete = findViewById(R.id.btn_delete);
        mBtnAdd.setOnClickListener(this);
        mBtnQuery.setOnClickListener(this);
        mBtnUpdate.setOnClickListener(this);
        mBtnDelete.setOnClickListener(this);


    }

    public void getRecyclerView() {
        RecyclerView recyclerView = findViewById(R.id.rv_shopping_mall);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 1);
        recyclerView.setLayoutManager(gridLayoutManager);

        ShoppingMallAdapter shoppingMallAdapter = new ShoppingMallAdapter(data, this);
        recyclerView.setAdapter(shoppingMallAdapter);
    }

    @Override
    public void onClick(View v) {
        String name, price, number;
        SQLiteDatabase db;
        ContentValues values;
        switch (v.getId()) {
            case R.id.btn_add: //添加数据
                name = mEtName.getText().toString();
                price = mEtPrice.getText().toString();
                number = mEtNumber.getText().toString();
                db = myHelper.getWritableDatabase();//获取可读写SQLiteDatabse对象
                values = new ContentValues();        //创建ContentValues对象
                values.put("name", name);             //将数据添加到ContentValues对象
                values.put("price", price);
                values.put("number", number);
                db.insert("information", null, values);
                Toast.makeText(this, "信息已添加", Toast.LENGTH_SHORT).show();
                db.close();
                break;
            case R.id.btn_query: //查询数据
                data.clear();
                db = myHelper.getReadableDatabase();
                Cursor cursor = db.query("information", null, null, null, null,
                        null, null);
                for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                    System.out.println(cursor.getString(1));
                    System.out.println(cursor.getString(2));
                    System.out.println(cursor.getString(3));


                    Goods goods = new Goods();
                    goods.setName(cursor.getString(1));
                    goods.setPrice(cursor.getString(2));
                    goods.setCount(cursor.getString(3));
                    data.add(goods);
                }

                getRecyclerView();






                if (cursor.getCount() == 0) {
                    mTvShow.setText("");
                    Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
                }
                /*else {
                    cursor.moveToFirst();
                    mTvShow.setText("🍬" + cursor.getString(1) + "🍬" +
                            "      " + cursor.getString(2) + "💰      " + cursor.getString(3) + "个🎯" + "\n");
                }

                while (cursor.moveToNext()) {
                    mTvShow.append("🍬" + cursor.getString(1) + "🍬" +
                            "      " + cursor.getString(2) + "💰      " + cursor.getString(3) + "个🎯" + "\n");
                }*/
                cursor.close();
                db.close();
                break;
            case R.id.btn_update: //修改数据
                db = myHelper.getWritableDatabase();
                values = new ContentValues();       // 要修改的数据
                values.put("price", price = mEtPrice.getText().toString());
                values.put("number", price = mEtNumber.getText().toString());
                db.update("information", values, "name=?",
                        new String[]{mEtName.getText().toString()}); // 更新并得到行数
                Toast.makeText(this, "信息已修改", Toast.LENGTH_SHORT).show();
                db.close();
                break;
            case R.id.btn_delete: //删除数据
                db = myHelper.getWritableDatabase();
                db.delete("information", null, null);
                Toast.makeText(this, "信息已删除", Toast.LENGTH_SHORT).show();
                mTvShow.setText("");
                db.close();
                break;
        }
    }

}